package com.red.wood.module.eval.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.dao.PublicOrderDao;

@Repository
public class PublicOrderDaoImpl implements PublicOrderDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(IMap param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = listPageData(param);
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}

	public List<Map<String, Object>> listPageData(IMap param){
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		sql.append("select t1.id,t2.name,t2.address,t2.number,t1.applyBusiness,t2.productionUnit,t2.gameCity,");
		sql.append("  case when t1.statue = '7' then '测试通过' when t1.statue = '8' then '测试不通过' end as statue,");
		sql.append(" (select count(1) from t_eval_applicationcdn_node node where t1.id = node.singleTaskId) as houseNodeNum,");
		sql.append(" (select count(2) from t_eval_application_coverinfo t where t.singleTaskId = t1.id) as linkNum");
		sql.append(" from t_eval_singletask t1");
		sql.append(" inner join t_eval_application_cdn t2");
		sql.append(" on t1.id = t2.singleTaskId");
		sql.append(" where 1 = 1 and t1.statue in (7,8)");
		sql.append(getQueryConf(param));
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(IMap param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count");
		sql.append(" from t_eval_singletask t1");
		sql.append(" inner join t_eval_application_cdn t2 ");
		sql.append(" on t1.id = t2.singleTaskId");
		sql.append(" where 1 = 1 and t1.statue in (7,8)");
		sql.append(getQueryConf(param));
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryConf(IMap param) {
		String name = CommonUtils.valueOf(param.get("name"));
		String address = CommonUtils.valueOf(param.get("address"));
		String number = CommonUtils.valueOf(param.get("number"));
		String statue = CommonUtils.valueOf(param.get("statue"));
		String productionUnit = CommonUtils.valueOf(param.get("productionUnit"));
		String gameCity = CommonUtils.valueOf(param.get("gameCity"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.isNotBlank(name)) {
			sql.append(" and t2.name like '%"+name+"%'");
		}
		if(StringUtils.isNotBlank(address)) {
			sql.append(" and t2.address like '%"+address+"%'");
		}
		if(StringUtils.isNotBlank(number)) {
			sql.append(" and t2.number like '%"+number+"%'");
		}
		if(!StringUtils.equals(statue, "all")) {
			sql.append(" and t1.statue like '%"+statue+"%'");
		}
		if(StringUtils.isNotBlank(productionUnit)) {
			sql.append(" and t2.productionUnit like '%"+productionUnit+"%'");
		}
		if(StringUtils.isNotBlank(gameCity)) {
			sql.append(" and t2.gameCity like '%"+gameCity+"%'");
		}
		return sql;
	}
}
